<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="付款单编号" prop="paymentNo">
        <el-input
          v-model="queryParams.paymentNo"
          placeholder="请输入付款单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择源单" prop="sourceForm">
        <el-input
          v-model="queryParams.sourceForm"
          placeholder="请输入选择源单"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款日期" prop="payDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.payDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择付款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择付款方式" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购负责人" prop="purchaseFollower">
        <el-input
          v-model="queryParams.purchaseFollower"
          placeholder="请输入采购负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本次付款金额" prop="payAcNow">
        <el-input
          v-model="queryParams.payAcNow"
          placeholder="请输入本次付款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单总额" prop="sourceAc">
        <el-input
          v-model="queryParams.sourceAc"
          placeholder="请输入源单总额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:payment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:payment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:payment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:payment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="付款单编号" align="center" prop="paymentNo" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="选择源单" align="center" prop="sourceForm" />
      <el-table-column label="付款日期" align="center" prop="payDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款方式" align="center" prop="payType" />
      <el-table-column label="采购负责人" align="center" prop="purchaseFollower" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="本次付款金额" align="center" prop="payAcNow" />
      <el-table-column label="源单总额" align="center" prop="sourceAc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:payment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:payment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改付款单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="付款单编号" prop="paymentNo">
          <el-input v-model="form.paymentNo" placeholder="请输入付款单编号" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="选择源单" prop="sourceForm">
          <el-input v-model="form.sourceForm" placeholder="请输入选择源单" />
        </el-form-item>
        <el-form-item label="付款日期" prop="payDate">
          <el-date-picker clearable size="small"
            v-model="form.payDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择付款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择付款方式">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="采购负责人" prop="purchaseFollower">
          <el-input v-model="form.purchaseFollower" placeholder="请输入采购负责人" />
        </el-form-item>
        <el-form-item label="本次付款金额" prop="payAcNow">
          <el-input v-model="form.payAcNow" placeholder="请输入本次付款金额" />
        </el-form-item>
        <el-form-item label="源单总额" prop="sourceAc">
          <el-input v-model="form.sourceAc" placeholder="请输入源单总额" />
        </el-form-item>
        <el-divider content-position="center">顾客产品信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCustomerProduct">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCustomerProduct">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="customerProductList" :row-class-name="rowCustomerProductIndex" @selection-change="handleCustomerProductSelectionChange" ref="customerProduct">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="产品名称" prop="productName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productName" placeholder="请输入产品名称" />
            </template>
          </el-table-column>
          <el-table-column label="产品名称" prop="productNameChange">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productNameChange" placeholder="请输入产品名称" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="quantity">
            <template slot-scope="scope">
              <el-input v-model="scope.row.quantity" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="价税合计" prop="taxRevenue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.taxRevenue" placeholder="请输入价税合计" />
            </template>
          </el-table-column>
          <el-table-column label="未出库数量" prop="uncheckoutNum">
            <template slot-scope="scope">
              <el-input v-model="scope.row.uncheckoutNum" placeholder="请输入未出库数量" />
            </template>
          </el-table-column>
          <el-table-column label="已出库数量" prop="checkoutNum">
            <template slot-scope="scope">
              <el-input v-model="scope.row.checkoutNum" placeholder="请输入已出库数量" />
            </template>
          </el-table-column>
          <el-table-column label="服务开始日" prop="serviceBdate">
            <template slot-scope="scope">
              <el-input v-model="scope.row.serviceBdate" placeholder="请输入服务开始日" />
            </template>
          </el-table-column>
          <el-table-column label="服务到期日" prop="serviceEdate">
            <template slot-scope="scope">
              <el-input v-model="scope.row.serviceEdate" placeholder="请输入服务到期日" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayment, getPayment, delPayment, addPayment, updatePayment } from "@/api/crm/finance/payment";

export default {
  name: "Payment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCustomerProduct: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款单表格数据
      paymentList: [],
      // 顾客产品信息表格数据
      customerProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paymentNo: null,
        supplierName: null,
        sourceForm: null,
        payDate: null,
        payType: null,
        purchaseFollower: null,
        payAcNow: null,
        sourceAc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询付款单列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        paymentNo: null,
        supplierName: null,
        sourceForm: null,
        payDate: null,
        payType: null,
        purchaseFollower: null,
        updateTime: null,
        payAcNow: null,
        sourceAc: null
      };
      this.customerProductList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayment(id).then(response => {
        this.form = response.data;
        this.customerProductList = response.data.customerProductList;
        this.open = true;
        this.title = "修改付款单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.customerProductList = this.customerProductList;
          if (this.form.id != null) {
            updatePayment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除付款单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPayment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
	/** 顾客产品信息序号 */
    rowCustomerProductIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 顾客产品信息添加按钮操作 */
    handleAddCustomerProduct() {
      let obj = {};
      obj.productName = "";
      obj.productNameChange = "";
      obj.quantity = "";
      obj.taxRevenue = "";
      obj.uncheckoutNum = "";
      obj.checkoutNum = "";
      obj.serviceBdate = "";
      obj.serviceEdate = "";
      this.customerProductList.push(obj);
    },
    /** 顾客产品信息删除按钮操作 */
    handleDeleteCustomerProduct() {
      if (this.checkedCustomerProduct.length == 0) {
        this.$alert("请先选择要删除的顾客产品信息数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.customerProductList.splice(this.checkedCustomerProduct[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleCustomerProductSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.customerProduct.clearSelection();
        this.$refs.customerProduct.toggleRowSelection(selection.pop());
      } else {
        this.checkedCustomerProduct = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/payment/export', {
        ...this.queryParams
      }, `crm_payment.xlsx`)
    }
  }
};
</script>
